Imports System.Configuration.ConfigurationManager

Partial Class Admin_QueryList
    Inherits System.Web.UI.Page

    Private _data_querylist As DS_querylist

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            LoadQuery()
        End If
        _data_querylist = Session.Item("DS_querylist")

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim dt_tmp As System.Data.DataView

        dt_tmp = New System.Data.DataView(_data_querylist.TBL_querylist)
        dt_tmp.Sort = "QRY_nome"
        GRV_querylist.DataSource = dt_tmp '_data_querylist.TBL_querylist
        GRV_querylist.DataBind()

    End Sub

    Private Sub LoadQuery()
 
        Session.Item("DS_querylist") = CType(Session.Item("DBA"), DBAccess).ReadDS_queryList
        Session.Item("DS_parametri") = CType(Session.Item("DBA"), DBAccess).ReadDS_ParametriSch

    End Sub

    Public Function DecodeStato(ByVal nCodeStato As Integer) As String
        Dim cRtn As String

        Select Case nCodeStato
            Case Is = 1
                cRtn = "In costruzione"
            Case Is = 2
                cRtn = "Attiva per gli utenti"
            Case Is = 3
                cRtn = "Sospesa"
        End Select
        Return cRtn

    End Function

    Protected Sub GRV_querylist_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GRV_querylist.RowEditing

        Session.Item("QRY_Selected") = _data_querylist.TBL_querylist.FindByQRY_codice(GRV_querylist.DataKeys(e.NewEditIndex).Value).QRY_codice
        Response.Redirect("QueryManagement.aspx")

    End Sub

    Protected Sub BTN_new_query_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_new_query.Click

        Session.Item("QRY_Selected") = -1
        Response.Redirect("QueryManagement.aspx")

    End Sub

    Protected Sub BTN_delete_query_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_delete_query.Click
        Dim lDeleted As Boolean = False
        Dim dr_qry As DS_querylist.TBL_querylistRow

        For Each Grv_Row As GridViewRow In GRV_querylist.Rows
            If CType(Grv_Row.FindControl("CHB_select"), CheckBox).Checked Then
                dr_qry = _data_querylist.TBL_querylist.FindByQRY_codice(GRV_querylist.DataKeys(Grv_Row.RowIndex).Value)
                dr_qry.Delete()
                lDeleted = True
            End If
        Next
        If lDeleted Then
            _data_querylist.TBL_querylist.AcceptChanges()
            CType(Session.Item("DBA"), DBAccess).WriteDS_queryList(_data_querylist)
        End If
        Session.Item("DS_querylist") = _data_querylist

    End Sub

    Public Function QuerySchedulata(ByVal nCodiceQuey As Integer) As Boolean

        Return _data_querylist.TBL_querysched.Select(String.Concat("QRH_cod_qry = ", nCodiceQuey)).Length > 0

    End Function


End Class
